//
//  PersonalSettingViewController.m
//  renmingongshe
//
//  Created by macbook on 2017/11/7.
//  Copyright © 2017年 xiaodian. All rights reserved.
//

#import "PersonalSettingViewController.h"
#import "PersonalEditController.h"

@interface PersonalSettingViewController ()
{
    NSDictionary *_data;
}
@property (weak, nonatomic) IBOutlet UILabel *processLb;
@property (weak, nonatomic) IBOutlet UIView *processView;
@property (weak, nonatomic) IBOutlet UIButton *editBtn;
@property (weak, nonatomic) IBOutlet UIImageView *imaView;
@property (weak, nonatomic) IBOutlet UILabel *name;
@property (weak, nonatomic) IBOutlet UILabel *sex;
@property (weak, nonatomic) IBOutlet UILabel *age;
@property (weak, nonatomic) IBOutlet UILabel *phone;
@property (weak, nonatomic) IBOutlet UILabel *weichat;

@end

@implementation PersonalSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.titleLabel.text = @"个人资料";
    self.view.backgroundColor = [UIColor colorWithHex:0xf2f2f2];
    _editBtn.layer.cornerRadius = 5;
    _editBtn.layer.borderWidth= 1;
    _editBtn.layer.borderColor = [UIColor darkGrayColor].CGColor;
    [self getData];
}
- (void)viewWillAppear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changUI) name:@"ChangUI" object:nil];
}
- (void)changUI
{
    [self getData];
}
//- (void)viewWillDisappear:(BOOL)animated
//{
//    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"ChangUI" object:nil];
//}
- (void)getData
{
    [THWManager PostHttp:API(MineInfo) Parameters:@{@"userToken":[Utils getUserToken]} Success:^(id responseObject) {
        
        if ([responseObject[@"status_code"]integerValue]) {
            
            if ([responseObject[@"status_code"]integerValue]==1008) {
                [self.view Message:responseObject[@"msg"]];
                dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC));
                dispatch_after(delayTime, dispatch_get_main_queue(), ^{
                    [self.navigationController popViewControllerAnimated:YES];
                });
            }else{
                _data = responseObject[@"data"];
                [self fillUI:responseObject[@"data"]];
            }
        }
        
    } andFalse:^(NSError *error) {
        
    }];
}
- (void)fillUI:(NSDictionary *)dic
{
    _age.text = [NSString stringWithFormat:@"%@",dic[@"userAge"]];
    _name.text = [NSString stringWithFormat:@"%@",dic[@"userName"]];
    _phone.text = [NSString stringWithFormat:@"%@",[TXUtilsString IsNull:dic[@"userPhone"]]?@"暂无":dic[@"userPhone"]];
    _weichat.text = [NSString stringWithFormat:@"%@",[TXUtilsString IsNull:dic[@"userWei"]]?@"暂无":dic[@"userWei"]];
    _sex.text = [dic[@"userSex"]integerValue]?@"男":@"女";
    [_imaView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://%@",dic[@"userPhoto"]]] placeholderImage:[UIImage imageNamed:@"morentouxiang"]];
    
    NSInteger com = [dic[@"complete"]integerValue];
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 0, _processView.width*com/100, _processView.height)];
    [_processView addSubview:line];
    line.backgroundColor = [UIColor greenColor];
    line.layer.cornerRadius = 5;
    
    _processLb.text = [NSString stringWithFormat:@"资料完善度% ld%%（资料完善度增加信用度)",com];
}
- (IBAction)editInfo:(id)sender {
    PersonalEditController *edit = [[PersonalEditController alloc]init];
    edit.data = _data;
    edit.back = ^(NSInteger i) {
        UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 0, _processView.width*i/100, _processView.height)];
        [_processView addSubview:line];
        line.backgroundColor = [UIColor greenColor];
        line.layer.cornerRadius = 5;
        
        _processLb.text = [NSString stringWithFormat:@"资料完善度% ld%%（资料完善度增加信用度)",i];
    };
    [self.navigationController pushViewController:edit animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
